package com.qiangzhi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qiangzhi.constant.MessageConstant;
import com.qiangzhi.pojo.dto.CollegeDTO;
import com.qiangzhi.pojo.dto.CollegePageQueryDTO;
import com.qiangzhi.exception.BaseException;
import com.qiangzhi.mapper.CollegeMapper;
import com.qiangzhi.pojo.po.CollegePO;
import com.qiangzhi.service.CollegeService;
import com.qiangzhi.pojo.vo.CollegeVO;
import com.qiangzhi.result.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollegeServiceImpl implements CollegeService {

    @Autowired
    private CollegeMapper collegeMapper;

    /**
     * 新增学院和班级
     *
     * @param collegeDTO
     * @return
     */
    public void insertCollegeOrMajor(CollegeDTO collegeDTO) {
        CollegePO collegePO = new CollegePO();
        BeanUtils.copyProperties(collegeDTO, collegePO);
        collegeMapper.insertCollegeOrMajor(collegePO);
    }

    /**
     * 根据id编辑学院和班级信息
     *
     * @param collegeDTO
     * @return
     */
    public void updateCollegeOrMajor(CollegeDTO collegeDTO) {
        CollegePO collegePO = new CollegePO();
        BeanUtils.copyProperties(collegeDTO, collegePO);
        collegeMapper.updateCollegeOrMajor(collegePO);
    }

    /**
     * 学院和班级分页查询
     *
     * @param collegePageQueryDTO
     * @return
     */
    public PageResult page(CollegePageQueryDTO collegePageQueryDTO) {
        // 直接用PageHelper进行分页查询 PageNum当前页页码 PageSize当前页条数
        PageHelper.startPage(collegePageQueryDTO.getPageNum(), collegePageQueryDTO.getPageSize());
        Page<CollegeVO> page = collegeMapper.page(collegePageQueryDTO);

        // 为每条数据赋值identify属性
        String identify = collegePageQueryDTO.getIdentify();
        page.forEach(userVO -> userVO.setIdentify(identify));

        long total = page.getTotal();
        List<CollegeVO> records = page.getResult();

        return new PageResult(total, records);
    }

    /**
     * 查询全部学院
     *
     * @return
     */
    public List<CollegeVO> getAllCollege() {
        return collegeMapper.getAllCollege();
    }

    /**
     * 根据学院id查询班级
     *
     * @param collegeId
     * @return
     */
    public List<CollegeVO> getMajorByCollegeId(Integer collegeId) {
        return collegeMapper.getMajorByCollegeId(collegeId);
    }

    /**
     * 根据id查询学院或班级详细信息
     *
     * @param collegeDTO
     * @return
     */
    public CollegeVO getDetail(CollegeDTO collegeDTO) {
        return collegeMapper.getDetail(collegeDTO);
    }

    /**
     * 根据id删除学院信息
     *
     * @param collegeId
     * @return
     */
    public void deleteByCollegeId(Integer collegeId) {
        // 删除前需要判断班级表有没有学院数据绑定
        Integer linkNum = collegeMapper.checkCollegeLink(collegeId);
        if (linkNum > 0) throw new BaseException(MessageConstant.LINK_DELETE_FAILED);

        collegeMapper.deleteByCollegeId(collegeId);
    }

    /**
     * 根据id删除班级信息
     *
     * @param majorId
     * @return
     */
    public void deleteByMajorId(Integer majorId) {
        // 删除前需要判断学生表有没有班级数据绑定
        Integer linkNum = collegeMapper.checkMajorLink(majorId);
        if (linkNum > 0) throw new BaseException(MessageConstant.LINK_DELETE_FAILED);

        collegeMapper.deleteByMajorId(majorId);
    }

}
